package projet.bd;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class Alarme extends Activity {
	
	// define the listener for when the user clicks the
	// Time Picker's Set button
	private TimePickerDialog.OnTimeSetListener theTimeSetListener
	      = new TimePickerDialog.OnTimeSetListener(){
	    
	    public void onTimeSet(TimePicker view, int hour, int minute) {
	      String time = "Heure : " + String.valueOf(hour) + "\n"
	                        + "Minute: " + String.valueOf(minute);
	      Toast.makeText(Alarme.this, time, Toast.LENGTH_LONG).show();
	      int theHour = hour;
	      int theMinute = minute;
	 
	      Intent alarmIntent = new Intent(Alarme.this, AlarmeService.class);
	      alarmIntent.putExtra("nel.example.alarme","alarme");
	      PendingIntent pendingAlarmIntent = PendingIntent.getService(Alarme.this, 0, alarmIntent, 0);
	 
	      AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
	 
	      Calendar AlarmCal = Calendar.getInstance();
	      AlarmCal.setTimeInMillis(System.currentTimeMillis());
	      AlarmCal.set(Calendar.HOUR_OF_DAY, theHour);  // set user selection
	      AlarmCal.set(Calendar.MINUTE, theMinute);        // set user selection
	      AlarmCal.set(Calendar.SECOND, 0);
	 
	      //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingAlarmIntent);
	      alarmManager.set(AlarmManager.RTC_WAKEUP, AlarmCal.getTimeInMillis(), pendingAlarmIntent);
	      
	      
	      // On envoie au stockage
	      
	      SimpleDateFormat nouveauDateFormat = new SimpleDateFormat(
					"dd/MM/yyyy");
		Date d = new Date();
		String date = nouveauDateFormat.format(d);
		String type = "alarme";
		String event = new String();
		
		event += "Type : " + type + "\n";
		event += "Date : " + date + " " + theHour + ":" + theMinute + ":00\n";

	      
	      Intent i = new Intent(Alarme.this, SmsStockageActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.putExtra("val", event);
			i.putExtra("type", "insertion");
			startActivity(i);
	   }
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm);		
		
	}
	
	public void pickTimeHandler(View target) {
	    showDialog(0);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	   final Calendar c = Calendar.getInstance();
	   int hour = c.get(Calendar.HOUR_OF_DAY);
	   int minute = c.get(Calendar.MINUTE);
	   return new TimePickerDialog(this,theTimeSetListener, hour, minute, false);
	 }
}